﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FangChanCrm.Services.Interface;
using FangChanCrmEF;
using FangChanCrmModel;
using Maticsoft.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using NPOI.SS.Formula.Functions;

namespace FangChanCrm.Controllers
{
    public class XinWenController : Controller
    {
        private readonly It_XinWenBiaoService t_XinWenBiaoService;
        private readonly It_RenYuanBiaoService t_RenYuanBiaoService;

        public XinWenController(It_XinWenBiaoService xinWenBiaoService, It_RenYuanBiaoService renYuanBiaoService, IConfiguration configuration)
        {
            t_XinWenBiaoService = xinWenBiaoService;
            t_RenYuanBiaoService = renYuanBiaoService;
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

       
        public IActionResult XinWenAdd()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }
        public IActionResult XinWenList()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }
        public IActionResult XinWenUpdate()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }

        public string XinWenAddFangFa()
        {
            int id = Convert.ToInt32(HttpContext.Session.GetString("loginid"));
            string gonggaoneirong = Request.Form["textArea3"];
            string XinWenBiaoTi = Request.Form["XinWenBiaoTi"];
            t_RenYuanBiao ry = t_RenYuanBiaoService.GetById(id);

            t_XinWenBiao gonggao = new t_XinWenBiao();
            gonggao.FangBuRenId = id;
            gonggao.XinWenNeiRong = gonggaoneirong;
            gonggao.FaBuShiJian = DateTime.Now.ToString();
            gonggao.XinWenDengJi = ry.t_GangWeiBiao.DengJi.ToString();
            gonggao.ZhuangTai = "启用";
            gonggao.BeiZhu1 = XinWenBiaoTi;
            int panduan = 0;
            panduan = t_XinWenBiaoService.XinWenAdd(gonggao);

            if (panduan > 0)
            {
                panduan = 1;
            }
            else
            {
                panduan = 0;
            }
            return panduan.ToString();
        }

        public string InitializationDataTable()
        {
            t_XinWenBiao t_XinWenBiao = new t_XinWenBiao();
            int id = Convert.ToInt32(HttpContext.Session.GetString("loginid"));
            if (id != 0) 
            {
                t_XinWenBiao.FangBuRenId = id;
            }
            return t_XinWenBiaoService.GetListWithFenYe(t_XinWenBiao, Convert.ToInt32(Request.Form["start"]), Convert.ToInt32(Request.Form["limit"]), Request.Form["dir"], Convert.ToInt32(Request.Form["column"]));
        }

        public string QiYong()
        {
            string id = Request.Form["id"];

            bool panduan = false;
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var xinwen = _context.t_XinWenBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(id));
                xinwen.ZhuangTai = "启用";
                panduan = _context.SaveChanges() > 0;
            }

            //t_XinWenBiao t_XinWenBiao = t_XinWenBiaoService.GetId(Convert.ToInt32(id));
            //t_XinWenBiao.ZhuangTai = "启用";
            //bool panduan = t_XinWenBiaoService.Update(t_XinWenBiao);
            if (panduan)
            {
                return "success";
            }
            else
            {
                return "err";
            }
        }

        public string GetXinWen()
        {
            List<t_XinWenBiao> xw = t_XinWenBiaoService.GetXinWenList();
            StringBuilder Json = new StringBuilder();
            if (xw.Count > 5)
            {
                for (int i = 0; i < 5; i++)
                {
                    if (xw[i].BeiZhu1.ToString().Length > 10)
                    {
                        Json.Append("<div onclick=XinWenXiangXi(" + xw[i].Id + ")>" + xw[i].BeiZhu1.ToString().Substring(0, 20) + "</div>");
                    }
                    else
                    {
                        Json.Append("<div onclick=XinWenXiangXi(" + xw[i].Id + ")>" + xw[i].BeiZhu1 + "</div>");
                    }
                }
            }
            else
            {
                for (int i = 0; i < xw.Count; i++)
                {
                    if (xw[i].BeiZhu1.ToString().Length>10)
                    {
                        Json.Append("<div onclick=XinWenXiangXi(" + xw[i].Id + ")>" + xw[i].BeiZhu1.ToString().Substring(0,20) + "</div>");
                    }
                    else
                    {
                        Json.Append("<div onclick=XinWenXiangXi(" + xw[i].Id + ")>" + xw[i].BeiZhu1 + "</div>");
                    }
                    
                }
            }
            return Json.ToString();
        }

        public string JinYong()
        {
            string id = Request.Form["id"];

            bool panduan = false;
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var xinwen = _context.t_XinWenBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(id));
                xinwen.ZhuangTai = "禁用";
                panduan = _context.SaveChanges() > 0;
            }

            //t_XinWenBiao t_XinWenBiao = t_XinWenBiaoService.GetId(Convert.ToInt32(id));
            //t_XinWenBiao.ZhuangTai = "禁用";
            //bool panduan = t_XinWenBiaoService.Update(t_XinWenBiao);
            if (panduan)
            {
                return "success";
            }
            else
            {
                return "err";
            }
        }

        public string Getid()
        {
            string id = Request.Form["id"];
            t_XinWenBiao t_XinWenBiao = t_XinWenBiaoService.GetId(Convert.ToInt32(id));
            string shuju = "{\"neiRong\":\"" + t_XinWenBiao.XinWenNeiRong + "\",\"beiZhu1\":\""+t_XinWenBiao.BeiZhu1+"\"}";
            return shuju;
        }

        public string Update()
        {
            string id = Request.Form["id"];
            string neirong = Request.Form["neirong"];
            string XinWenBiaoTi = Request.Form["XinWenBiaoTi"];

            bool panduan = false;
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var xinwen = _context.t_XinWenBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(id));
                xinwen.XinWenNeiRong = neirong;
                xinwen.BeiZhu1 = XinWenBiaoTi;
                panduan = _context.SaveChanges() > 0;
            }

            //t_XinWenBiao t_XinWenBiao = t_XinWenBiaoService.GetId(Convert.ToInt32(id));
            //t_XinWenBiao.XinWenNeiRong = neirong;
            //t_XinWenBiao.BeiZhu1 = XinWenBiaoTi;
            //bool panduan = t_XinWenBiaoService.Update(t_XinWenBiao);

            if (panduan)
            {
                return "1";
            }
            else
            {
                return "0";
            }
        }

        public string XinWenXiangXi() 
        {
            string id = Request.Form["id"];
            t_XinWenBiao xw = t_XinWenBiaoService.GetId(Convert.ToInt32(id));
            t_RenYuanBiao ry = t_RenYuanBiaoService.GetById(xw.FangBuRenId);
            DateTime shijian = Convert.ToDateTime(xw.FaBuShiJian);
            string shijianshuju = shijian.ToString("yyyy/HH/mm");
            string json = "[{\"BiaoTi\":\""+xw.BeiZhu1+"\",\"ShiJian\":\""+ shijianshuju + "\",\"FaBuRen\":\""+ ry.XingMing + "\",\"NeiRong\":\""+xw.XinWenNeiRong+"\"}]";
            return json;
        }
    }
}
